require "base_def"

local web_login_api = {}

local function get_service_version_func(args)
    log.info('get_service_version_func req=', args)
    --渠道状态,客服链接,登录选项,绑定送金币
    local req = {'status', 'customer_service_url', 'login_type_switch', 'gold_for_bind_phone', 'other_config'}  
    local ret = CallMysqlRpc('GetChannelInfo', args.channel, req)
    if ret and ret.other_config then
        local ok, other_config = pcall(cjson.decode, ret.other_config)
        if ok then
            ret.acc_login_for_phone_switch = other_config
        end
    end
    local bindPhoneGiveCoinStatus = 0
    if ret and ret.gold_for_bind_phone and ret.gold_for_bind_phone > 0 then
        bindPhoneGiveCoinStatus = 1
    end
    local res = {
        status = ret.status,
        serviceLink = ret.customer_service_url,
        login_type_switch = ret.login_type_switch,
        bindPhoneGiveCoin = ret.gold_for_bind_phone,
        bindPhoneGiveCoinStatus = bindPhoneGiveCoinStatus,
        acc_login_for_phone_switch = ret.acc_login_for_phone_switch
    }
    res.downloadUrl = ''
 
    --这里是检测服务器状态1正常2只允许白名单进,为1,2都让进为2的时候还要在登录的时候判断一下是否为白名单  
    local status = CallMysqlRpc("CheckServiceStatus")
    if not (status == 1 or status == 2) then
        res.status = 0
    end
    
    --获取版本列表
    local version_list = CallMysqlRpc("ClientGetVersionList", {user_id = args.user_id})
    if version_list then 
        res.version_list = version_list
    else 
        log.error("CallMysqlRpc ClientGetVersionList error!")
    end

    return res
end

--取版本号
local function get_service_version(req_json)
    --xxtea解密
    local deok
    deok, req_json = pcall(XteaDecode, req_json)
    if not deok then
        log.info('get_service_version decode json error:', req_json)
        return
    end
    --log.info('get_service_version req_json:', req_json)
    local res = {ok = 0, info = ''}
    --解json
    local ok, req = pcall(cjson.decode, req_json)
    if not ok then
        if not req or not req.channel then
            res.err_msg = 'channel is null'
            log.error('json.decode channel is nil!')
        else
            log.error('get_service_version req_json decode failed!')
        end
        return encode(cjson.encode(res))
    else
        local info = get_service_version_func(req)
        if info then
            res.ok = 1
            res.info = info
        end
    end
    log.info('get_service_version res:', res)

    --xxtea加密返回
    return XteaEncode(cjson.encode(res))
end

--获取中心服的地址列表
local function get_center_list()                       
    local res = {
        data = {
            url = "ws://192.168.31.153:8502",
            version = '1.0.1'
        }
    }
    res.ret = 0
    local ret = cjson.encode(res)
    return XteaEncode(ret)
end

--获取验证码
local function get_verification(req_json)  
    log.info("get_verification req_json=", req_json)
    local deok
    deok, req_json = pcall(XteaDecode, req_json)
    if not deok then
        log.error('get_verification decode json error:', req_json)
        return
    end
    local res = {ok = 1}
    local ok, req = pcall(cjson.decode, req_json)
    if not ok then
        return XteaEncode(cjson.encode({ok = 0}))
    else
        log.info("req=", req)
        local ok, ret = pcall(cluster.call, "mysql", ".short_message", "SendVerifyMessage", req)
        if not ret then
            res = {ok = 0, err_msg = '发送验证码失败'}
        else
            if ret == -1 then
                local err_msg = '验证码请求过于频繁，请稍后再试'
                res = {ok = 0, err_msg = err_msg}
            end
        end
    end
    return XteaEncode(cjson.encode(res)) 
end

--加上括号会导致提前运行？
web_login_api.get_request_api = {
    --取版本号
    ["/get_service_version"] = get_service_version,
    --获取中心服的地址列表
    ["/list"] = get_center_list,
    --获取验证码
    ["/get_verification"] = get_verification,
    --修改密码
    ["/retrieve_password"] = retrieve_password,
    --请求指定后台游戏版本信息
    ["/get_game_version_msg"] = get_game_version_msg, 
     --游戏客户端添加后台对比版本信息
    ["/set_compare_version_msg"] = set_compare_version_msg,
     --游戏客户端重新设置游戏版本信息
    ["/reset_game_version_msg"] = reset_game_version_msg,
}

return  web_login_api

